package com.example.gkcircle.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.avos.avoscloud.AVUser;
import com.example.gkcircle.R;
import com.example.gkcircle.bean.Comment;
import com.example.gkcircle.utils.UniversalImageLoader;

import java.util.List;

/**
 * Created by Jehu on 16/4/13.
 */
public class MovieCommentAdapter extends BaseAdapter{

    private List<Comment> mData;
    private int commentCount;//评论总数
    private Activity mActivity;
    private LayoutInflater inflater;
    private final int MAX_TYPE=2;
    private final int FIRST_ITEM=0;//楼层数
    private final int NORMAL_ITEM=1;


    public MovieCommentAdapter(Activity activity,List<Comment> data,int commentCount){
        this.mData=data;
        this.commentCount=commentCount;
        this.mActivity=activity;
        this.inflater = LayoutInflater.from(mActivity);
    }

    @Override
    public int getCount() {
        if(mData==null ||mData.size()==0){
            return 1;
        }
        return mData.size()+1;
    }

    @Override
    public int getViewTypeCount() {
        return MAX_TYPE;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position){
            case FIRST_ITEM:
                return FIRST_ITEM;
            default:
                return NORMAL_ITEM;
        }
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        ImageView avatar;
        TextView username;
        TextView date;
        TextView floors;
        TextView content;
        ImageView topBg;
        ImageView bottomBg;
        ImageView dividerBg;
        TextView commentCount;
    }

    @Override
    public boolean isEnabled(int position) {
        if(position==0){
            return false;
        }
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int currentItem=getItemViewType(position);
        //第一层布局
        if(currentItem==FIRST_ITEM){
            final ViewHolder firstItemHolder;
            if(convertView==null){
                firstItemHolder=new ViewHolder();
                if(mData==null || mData.size()==0){
                    convertView=inflater.inflate(R.layout.comment_nothing_item,null);
                }else{
                    convertView=inflater.inflate(R.layout.comment_first_item,null);
                    firstItemHolder.commentCount=(TextView)convertView.findViewById(R.id.comment_first_item_count);
                    firstItemHolder.commentCount.setText(commentCount+"层");
                }
                convertView.setTag(firstItemHolder);
            }else{
                if(mData!=null || mData.size()!=0){
                    firstItemHolder=new ViewHolder();
                    convertView=inflater.inflate(R.layout.comment_first_item,null);
                    firstItemHolder.commentCount=(TextView)convertView.findViewById(R.id.comment_first_item_count);
                    firstItemHolder.commentCount.setText(commentCount+"层");
                    return convertView;
                }
                firstItemHolder=(ViewHolder)convertView.getTag();
            }
            return convertView;
        }else if(currentItem==NORMAL_ITEM){
            final ViewHolder viewHolder;

            if(convertView==null){
                viewHolder=new ViewHolder();
                convertView=inflater.inflate(R.layout.comment_item,null);
                viewHolder.topBg=(ImageView)convertView.findViewById(R.id.comment_item_top_bg);
                viewHolder.bottomBg=(ImageView)convertView.findViewById(R.id.comment_item_bottom_bg);
                viewHolder.dividerBg=(ImageView)convertView.findViewById(R.id.comment_item_divider);
                viewHolder.avatar=(ImageView)convertView.findViewById(R.id.comment_item_avatar);
                viewHolder.username=(TextView)convertView.findViewById(R.id.comment_item_username);
                viewHolder.date=(TextView)convertView.findViewById(R.id.comment_item_date);
                viewHolder.content=(TextView)convertView.findViewById(R.id.comment_item_content);
                viewHolder.floors=(TextView)convertView.findViewById(R.id.comment_item_floors);
                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder)convertView.getTag();
            }
            if(position==mData.size()){
                viewHolder.bottomBg.setVisibility(View.GONE);
            }
            Comment comment=mData.get(position-1);
            final AVUser user=comment.getUser();
            viewHolder.content.setText(comment.getContent());

            viewHolder.floors.setText((commentCount+1-position)+"#");
            viewHolder.username.setText(user.get("nickname").toString());
            viewHolder.content.setText(comment.getContent());
            viewHolder.date.setText(comment.getCreatedAt());
            //用户头像
            if(null!=user.getAVFile("avatar")){
                int width=mActivity.getResources().getDimensionPixelSize(R.dimen.movie_avatar_width);
                //获取缩略图url
                String avatarUrl=user.getAVFile("avatar").getThumbnailUrl(true,width,width);
                UniversalImageLoader.getIntance().displayImage(avatarUrl, viewHolder.avatar, UniversalImageLoader.getAvatarOptions(), UniversalImageLoader.getAnimateFirstDisplayListener());
            }else{
                viewHolder.avatar.setImageResource(R.mipmap.personal_default_user_icon);
            }

        }
        return convertView;
    }
}
